//
//  CityWeather.swift
//  ZhiYunWeather
//
//  Created by Wade on 2024/7/9.
//

import Foundation
import UIKit
import LGSideMenuController
import Alamofire
class KEMenuDetail : KYBaseVC ,UICollectionViewDelegate,UICollectionViewDataSource{
    var menuList:Array<KELetterDetail>? = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .MainBGColor
//        self.navigationController?.setNavigationBarHidden(false, animated: false)
        setupSubViews()
        self.collectionView?.register(KEDetailCollectionCell.self, forCellWithReuseIdentifier: "KEDetailCollectionCell")
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
       loadData()
    }
    
    func loadData(){
        let param = ["letter_type":0]
        AF.request("https://fc-mp-d6f030db-0e27-458b-906b-ae7d21ab9004.next.bspapp.com/getWords/getLettersByQuery",method: .post,parameters: param, encoding: JSONEncoding.default).responseDecodable(of: KELetterResult.self) { res in
            switch res.result {
            case .success(let result):
                let letters = result.result
                if letters!.count > 0 {
                    self.menuList = letters;
                    DispatchQueue.main.async {
                        self.collectionView?.reloadData()
                    }
                }
            case .failure(_):
                print("error")
                
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        guard let list = self.menuList else { return 0 }
        return list.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let list = self.menuList else { return UICollectionViewCell.init() }
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KEDetailCollectionCell", for: indexPath) as! KEDetailCollectionCell
        cell.priceLabel?.text = list[indexPath.item].letter
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let list = self.menuList else {
            return;
        }
        let vc = KEAlphabetNewDetail()
        vc.content = list[indexPath.item].letter;
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
}
